package com.cortex.pineapple.view;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.cortex.pineapple.R;
import com.cortex.pineapple.adapters.FilterAdapter;
import com.cortex.pineapple.fetch.RequestData.FILTER_TYPE;
import com.cortex.pineapple.fetch.RequestData.FILTER_TYPE.FILTER_VALUE;

public class FiltersView extends FrameLayout implements OnItemSelectedListener{

	private Spinner filesType;
	private Spinner imagesType,imagesSize,imagesColor,imagesPeople;
	private Spinner videoLength,videoResolution;
	private Spinner newsDate,newsCategory;
	private OnSelectedFilterListener onSelectedFilterListener;
	private RelativeLayout clear;
	private Animation leftIn,leftOut;

	public FiltersView(Context context) {
		super(context);
		init();
	}
	public FiltersView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	public FiltersView(Context context, AttributeSet attrs,int style) {
		super(context, attrs,style);
		init();
	}
	private void init(){
		LayoutInflater.from(getContext()).inflate(R.layout.view_filters, this);
		filesType = (Spinner)findViewById(R.id.filesType);
		imagesType = (Spinner)findViewById(R.id.imagesType);
		imagesSize = (Spinner)findViewById(R.id.imagesSize);
		imagesColor = (Spinner)findViewById(R.id.imagesColor);
		imagesPeople = (Spinner)findViewById(R.id.imagesPeople);
		videoLength = (Spinner)findViewById(R.id.videoLength);
		videoResolution = (Spinner)findViewById(R.id.videoResolution);
		newsDate = (Spinner)findViewById(R.id.newsDate);
		newsCategory = (Spinner)findViewById(R.id.newsCategory);
		clear = (RelativeLayout)findViewById(R.id.clear);
		buildAdapters();
		initListeners();

		leftIn = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_and_fade_in);
		leftOut = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_and_fade_out);
	}
	private String[] getFiltersVideoByResolution(){
		DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
		if(metrics.densityDpi<DisplayMetrics.DENSITY_MEDIUM){
			return getResources().getStringArray(R.array.resolution_low_recommend_arrays);
		}
		else if(metrics.densityDpi>=DisplayMetrics.DENSITY_MEDIUM &&
				metrics.densityDpi<DisplayMetrics.DENSITY_HIGH){
			return getResources().getStringArray(R.array.resolution_medium_recommend_arrays);
		}
		else if(metrics.densityDpi>=DisplayMetrics.DENSITY_HIGH){
			return getResources().getStringArray(R.array.resolution_high_recommend_arrays);
		}
		return null;
	}
	private String[] getFiltersImageSizeByResolution(){
		DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
		if(metrics.densityDpi<DisplayMetrics.DENSITY_MEDIUM){
			return getResources().getStringArray(R.array.images_size_small_recommend_arrays);
		}
		else if(metrics.densityDpi>=DisplayMetrics.DENSITY_MEDIUM &&
				metrics.densityDpi<DisplayMetrics.DENSITY_HIGH){
			return getResources().getStringArray(R.array.images_size_medium_recommend_arrays);
		}
		else if(metrics.densityDpi>=DisplayMetrics.DENSITY_HIGH){
			return getResources().getStringArray(R.array.images_size_large_recommend_arrays);
		}
		return null;
	}
	private void buildAdapters(){
		filesType.setAdapter(new FilterAdapter(getContext(), 
				getResources().getStringArray(R.array.files_type_arrays),getResources().getString(R.string.fileType)));
		imagesType.setAdapter(new FilterAdapter(getContext(),
				getResources().getStringArray(R.array.images_type_arrays),getResources().getString(R.string.imageType)));
		imagesSize.setAdapter(new FilterAdapter(getContext(),
				getFiltersImageSizeByResolution(),getResources().getString(R.string.size)));
		imagesColor.setAdapter(new FilterAdapter(getContext(),
				getResources().getStringArray(R.array.images_color_arrays),getResources().getString(R.string.color)));
		imagesPeople.setAdapter(new FilterAdapter(getContext(),
				getResources().getStringArray(R.array.images_people_arrays),getResources().getString(R.string.people)));
		videoLength.setAdapter(new FilterAdapter(getContext(), 
				getResources().getStringArray(R.array.video_length_arrays),getResources().getString(R.string.length)));
		videoResolution.setAdapter(new FilterAdapter(getContext(), 
				getFiltersVideoByResolution(),getResources().getString(R.string.resolution)));
		newsDate.setAdapter(new FilterAdapter(getContext(), 
				getResources().getStringArray(R.array.news_time_arrays),getResources().getString(R.string.date)));
		newsCategory.setAdapter(new FilterAdapter(getContext(), 
				getResources().getStringArray(R.array.news_category_arrays),getResources().getString(R.string.newsCategory)));
	}
	private void initListeners(){
		filesType.setOnItemSelectedListener(this);
		imagesType.setOnItemSelectedListener(this);
		imagesSize.setOnItemSelectedListener(this);
		imagesColor.setOnItemSelectedListener(this);
		imagesPeople.setOnItemSelectedListener(this);
		videoLength.setOnItemSelectedListener(this);
		videoResolution.setOnItemSelectedListener(this);
		newsDate.setOnItemSelectedListener(this);
		newsCategory.setOnItemSelectedListener(this);
		clear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				buildAdapters();
				if(onSelectedFilterListener != null){
					HashMap<FILTER_TYPE, FILTER_VALUE> filters = getSelectedFilters();
					onSelectedFilterListener.selectedFilter(null, filters);

				}
			}
		});
	}
	public void setOnSelectedFilterListener(OnSelectedFilterListener onSelectedFilterListener) {
		this.onSelectedFilterListener = onSelectedFilterListener;
	}
	private void addFilter(HashMap<FILTER_TYPE, FILTER_VALUE> filters,FILTER_TYPE filterType,String value){
		FILTER_VALUE filterValue = FILTER_VALUE.getFilterValue(value);
		if(filterValue!=null){
			filters.put(filterType, filterValue);
		}
	}
	public HashMap<FILTER_TYPE, FILTER_VALUE> getSelectedFilters(){
		HashMap<FILTER_TYPE, FILTER_VALUE> filters = new HashMap<FILTER_TYPE, FILTER_VALUE>();

		if(filesType.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.FILE_TYPE, (String) filesType.getSelectedItem());
		}
		if(imagesType.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.IMAGE_TYPE, (String) imagesType.getSelectedItem());
		}
		if(imagesSize.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.IMAGE_SIZE, (String) imagesSize.getSelectedItem());
		}
		if(imagesColor.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.IMAGE_COLOR, (String) imagesColor.getSelectedItem());
		}
		if(imagesPeople.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.IMAGE_PEOPLE, (String) imagesPeople.getSelectedItem());
		}
		if(videoLength.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.VIDEO_LENGTH, (String) videoLength.getSelectedItem());
		}
		if(videoResolution.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.VIDEO_RESOLUTION, (String) videoResolution.getSelectedItem());
		}
		if(newsDate.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.NEWS_DATE, (String) newsDate.getSelectedItem());
		}
		if(newsCategory.getVisibility() == VISIBLE){
			addFilter(filters, FILTER_TYPE.NEWS_CATEGORY, (String) newsCategory.getSelectedItem());
		}
		return filters;
	}
	@Override
	public void onItemSelected(AdapterView<?> adapterView, View view, int position,
			long id) {
		HashMap<FILTER_TYPE, FILTER_VALUE> filterChanged = new HashMap<FILTER_TYPE, FILTER_VALUE>();
		String value = (String) adapterView.getAdapter().getItem(position);
		View selectedView = adapterView.getSelectedView();
		if(selectedView == null){
			return;
		}
		String selectedText = (String) ((TextView)adapterView.getSelectedView()).getText();

		if(filesType == adapterView){
			addFilter(filterChanged, FILTER_TYPE.FILE_TYPE, value);
		}
		else if(imagesType == adapterView){
			addFilter(filterChanged, FILTER_TYPE.IMAGE_TYPE, value);
		}
		else if(imagesSize == adapterView){
			addFilter(filterChanged, FILTER_TYPE.IMAGE_SIZE, value);
		}
		else if(imagesColor == adapterView){
			addFilter(filterChanged, FILTER_TYPE.IMAGE_COLOR, value);
		}
		else if(imagesPeople == adapterView){
			addFilter(filterChanged, FILTER_TYPE.IMAGE_PEOPLE, value);
		}
		else if(videoLength == adapterView){
			addFilter(filterChanged, FILTER_TYPE.VIDEO_LENGTH, value);
		}
		else if(videoResolution == adapterView){
			addFilter(filterChanged, FILTER_TYPE.VIDEO_RESOLUTION, value);
		}
		else if(newsDate == adapterView){
			addFilter(filterChanged, FILTER_TYPE.NEWS_DATE, value);
		}
		else if(newsCategory == adapterView){
			addFilter(filterChanged, FILTER_TYPE.NEWS_CATEGORY, value);
		}

		if(onSelectedFilterListener != null){
			HashMap<FILTER_TYPE, FILTER_VALUE> filters = getSelectedFilters();
			if(!filterChanged.isEmpty() && !filters.isEmpty()
					|| selectedText.equals(getResources().getString(R.string.all))){
				onSelectedFilterListener.selectedFilter(filterChanged, filters);
			}

		}
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}
	private void setVisibleFilter(View filter,boolean isVisible){
		if(filter.getVisibility() == GONE && isVisible){
			filter.startAnimation(leftIn);
		}
		else if(filter.getVisibility() == VISIBLE && !isVisible){
			filter.startAnimation(leftOut);
		}
		filter.setVisibility(isVisible?VISIBLE:GONE);
	}
	public void setVisibleFilesType(boolean isVisible){
		setVisibleFilter(filesType, isVisible);
	}
	public void setVisibleImageType(boolean isVisible){
		setVisibleFilter(imagesType, isVisible);
	}
	public void setVisibleImageSize(boolean isVisible){
		setVisibleFilter(imagesSize, isVisible);
	}
	public void setVisibleImageColor(boolean isVisible){
		setVisibleFilter(imagesColor, isVisible);
	}
	public void setVisibleImageFace(boolean isVisible){
		setVisibleFilter(imagesPeople, isVisible);
	}
	public void setVisibleVideoLength(boolean isVisible){
		setVisibleFilter(videoLength, isVisible);
	}
	public void setVisibleVideoResolution(boolean isVisible){
		setVisibleFilter(videoResolution, isVisible);
	}
	public void setVisibleNewsTime(boolean isVisible){
		setVisibleFilter(newsDate, isVisible);
	}
	public void setVisibleNewsCategory(boolean isVisible){
		setVisibleFilter(newsCategory, isVisible);
	}
	public void setVisibleClear(boolean isVisible){
		setVisibleFilter(clear, isVisible);
	}
	public void setVisibleAllFilters(boolean isVisible){
		setVisibleFilesType(isVisible);
		setVisibleImageType(isVisible);
		setVisibleImageSize(isVisible);
		setVisibleImageColor(isVisible);
		setVisibleImageFace(isVisible);
		setVisibleVideoLength(isVisible);
		setVisibleVideoResolution(isVisible);
		setVisibleNewsTime(isVisible);
		setVisibleNewsCategory(isVisible);
	}
	public interface OnSelectedFilterListener{
		void selectedFilter(HashMap<FILTER_TYPE, FILTER_VALUE> filterChanged,
				HashMap<FILTER_TYPE, FILTER_VALUE> filters);
	}
	public void clear(){
		buildAdapters();
	}
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		filesType.setEnabled(enabled);
		imagesType.setEnabled(enabled);
		imagesSize.setEnabled(enabled);
		imagesColor.setEnabled(enabled);
		imagesPeople.setEnabled(enabled);
		videoLength.setEnabled(enabled);
		videoResolution.setEnabled(enabled);
		newsDate.setEnabled(enabled);
		newsCategory.setEnabled(enabled);
		clear.setEnabled(enabled);
	}
}
